package com.xw.comm.controller;

import com.xw.comm.entity.BaseResponse;
import com.xw.comm.entity.CobraResultCodeConstant;
import com.xw.comm.entity.ResultCode;
import com.xw.comm.service.PythonRunService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：xiongwei
 * @date ：Created in 2021/3/2 23:37
 */
@Slf4j
@RestController
@RequestMapping("/python")
@Api(value = "PythonRunController|python调用接口")
public class PythonRunController {

    @Autowired
    private PythonRunService pythonRunService;

    @GetMapping(value = "/pythonRun")
    @ApiOperation(value = "测试页面 ", notes = "测试接口")
    public BaseResponse testPage() {
        ResultCode resultCode = new ResultCode(CobraResultCodeConstant.SUCCESS.getCode(),"pythonRun");
        pythonRunService.runWebTest();
        log.info("pythonRun = {}", "pythonRun测试页面");
        return new BaseResponse(resultCode);
    }

    @GetMapping(value = "/runCommand")
    @ApiOperation(value = "执行命令")
    @ApiImplicitParam(name = "command", value = "命令", required = true, dataType = "String", paramType = "query")
    public BaseResponse runCommand(@RequestParam String command) {
        ResultCode resultCode = new ResultCode(CobraResultCodeConstant.SUCCESS.getCode(),"runCommand");
        pythonRunService.runAbsolutelyCommand(command);
        return new BaseResponse(resultCode);
    }

    @GetMapping(value = "/runPathFile")
    @ApiOperation(value = "执行python文件")
    @ApiImplicitParam(name = "filePath", value = "文件地址", required = true, dataType = "String", paramType = "query")
    public BaseResponse runPathFile(@RequestParam String filePath) {
        ResultCode resultCode = new ResultCode(CobraResultCodeConstant.SUCCESS.getCode(),"runPathFile");
        pythonRunService.runPythonFile(filePath);
        return new BaseResponse(resultCode);
    }

}
